import * as THREE from "three";
const animationFrameIds = new Map<THREE.Object3D, number>();

// 旋转动画函数
export function animateRotation(object: THREE.Object3D, axis: "x" | "y" | "z", speed: number) {
    function rotate() {
        object.rotation[axis] += speed; // 以固定速度更新旋转
        const frameId = requestAnimationFrame(rotate);  // 请求下一帧，并保存其ID
        animationFrameIds.set(object, frameId); // 将Object与其对应的动画帧ID关联
    }
    rotate(); // 启动旋转
}
//取消旋转动画
export function stopRotation(object: THREE.Object3D) {
    const frameId = animationFrameIds.get(object);
    if (frameId !== undefined) {
        cancelAnimationFrame(frameId); // 取消当前Object的旋转动画
        animationFrameIds.delete(object); // 从Map中移除该Object
    }
}


//物体启动状态
export function animateColordefaultrun(object: { material: { color: { [x: string]: any; r: number; g: number; b: number; }; }; }, initialColor: { r: number; g: number; b: number; }, targetColor: THREE.Color, duration: number, run: number) {
    if (!object || !object.material) return;


    let startTime: number | null = null;
    //const initialMaterial = object.material;
    // const targetMaterial = new THREE.MeshPhongMaterial({ color: targetColor }); // 目标材质
    const targetMaterial = new THREE.MeshStandardMaterial({
        color: targetColor,
        metalness: 0.9,  // 设置为 1.0 以实现完全的金属效果
        roughness: 0.29   // 设置粗糙度，0.0 为镜面，1.0 为完全粗糙
    });

    const targetMaterialx = new THREE.MeshStandardMaterial({
        color: 0xF5F5F6,
        metalness: 0.9,  // 设置为 1.0 以实现完全的金属效果
        roughness: 0.29   // 设置粗糙度，0.0 为镜面，1.0 为完全粗糙
    });

    function animateColor(time: number) {
        if (!startTime) startTime = time;

        const elapsedTime = time - startTime;
        const fraction = Math.min(elapsedTime / duration, 1);

        // 插值颜色
        object.material.color.r = THREE.MathUtils.lerp(initialColor.r, targetColor.r, fraction);
        object.material.color.g = THREE.MathUtils.lerp(initialColor.g, targetColor.g, fraction);
        object.material.color.b = THREE.MathUtils.lerp(initialColor.b, targetColor.b, fraction);

        if (fraction < 1) {
            requestAnimationFrame(animateColor);
        }
    }

    if (run === 1) {
        object.material = targetMaterial;
        requestAnimationFrame(animateColor);
    } else {
        object.material = targetMaterialx;
        //object.material.color.set(initialColor);
    }
}
// 创建并设置液体的固定高度
const tankToLiquidMeshMap = new Map<THREE.Object3D, THREE.Mesh>();

export function createFixedLiquidLevel(
    tank: THREE.Object3D, 
    model: THREE.Group, 
    heightRatio: number, 
    color: number = 0x0000ff, 
    opacity: number = 0.5
) {
    // 获取罐体的内部尺寸
    const boundingBox = new THREE.Box3().setFromObject(tank);
    const size = new THREE.Vector3();
    boundingBox.getSize(size);

    // 创建液体几何体
    const liquidGeometry = new THREE.CylinderGeometry(size.x / 2, size.x / 2, size.y, 32);
    const liquidMaterial = new THREE.MeshPhongMaterial({ color, opacity, transparent: true });
    const liquidMesh = new THREE.Mesh(liquidGeometry, liquidMaterial);

    // 计算并设置液体的高度
    const liquidHeight = size.y * heightRatio;
    liquidMesh.scale.y = liquidHeight / size.y;
    liquidMesh.position.set(
        boundingBox.min.x + size.x / 2,
        boundingBox.min.y + liquidHeight / 2,
        boundingBox.min.z + size.z / 2
    );

    // 添加液体到模型
    model.add(liquidMesh);

    // 保存 tank 和对应的 liquidMesh，以便以后删除
    tankToLiquidMeshMap.set(tank, liquidMesh);
}

export function removeLiquidLevel(tank: THREE.Object3D) {
    const liquidMesh = tankToLiquidMeshMap.get(tank);
    if (liquidMesh) {
        const parent = liquidMesh.parent;
        if (parent) {
            parent.remove(liquidMesh); // 从模型中移除液体
        }
        tankToLiquidMeshMap.delete(tank); // 从 Map 中移除 tank 和 liquidMesh 的关联
    }
}